import {
    resources,
    excelUtil as ExcelUtil
} from '../../../../../util/';
import { survey as SurveyApi } from '../../../../../config/request.js';
const eType = resources.eType;
module.exports = {
    name: 'list',
    data() {
        return {
            leftEye: '',
            eType: eType,
            tableCount: resources.tableCount,
            layout: resources.layout,
            city: resources.city,
            fields: resources.fields,
            dialogDetailVisible: false,
            detailData: {},
            downloadName: '用户问卷-' + new Date().getTime() + '.xlsx',
            status: {
                info: {
                    prop: 'status',
                    label: '问卷状态',
                    sortable: true
                },
                filter: {
                    list:[ 
                        { value: 1, key: '未完成', label: '未完成', text: '未完成' },
                        { value: 2, key: '完成', label: '完成', text: '完成' }],
                    multiple: false    
                },
                
                style: {
                    width: '100',
                    align: 'center'
                }
            },
        }
    },
    methods: {
        downloadExl(type) {
            if (type == 1) {
                this.downloadName = '用户问卷-' + new Date().getTime() + '.xlsx';
                var tableData = this.$store.state.survey.tableData;
                var data = ExcelUtil.formatterData(tableData, ['answer', 'status', 'images', 'age']);
                ExcelUtil.downloadExl(this.downloadName, data);
            } else if (type == 2) {
                this.downloadName = this.detailData.name + ExcelUtil.formatterEtype(this.detailData.etype) +
                    new Date().getTime() + '.xlsx';
                var tableData = this.detailData.answer;
                var data = ExcelUtil.formatterData(tableData, ['qid', 'valueid'], { 'value': '答案' });
                ExcelUtil.downloadExl(this.downloadName, data);
            }
        },
        downloadExlAll() {
            SurveyApi.getAllSurveys.call(this, data => {
                var tableData = data['data'];
                if (!tableData) {
                    return;
                }
                var data = ExcelUtil.formatterData(tableData, ['answer', 'status', 'images', 'age']);
                this.generateExl('全部问卷信息-' + new Date().getTime() + '.xlsx', data);
            })
        },
        generateExl(name, data) {
            ExcelUtil.downloadExl(name, data);
        },
        /**
         * 格式化性别
         */
        formatterSex(item) {
            return item.sex == 1 ? '男' : '女';
        },

        /**
         * 格式化状态
         */
        formatterStatus(item) {
            return item.status == 1 ? '未完成' : '完成';
        },
        /**
         * 过滤性别
         * 
         * @param {any} value 
         * @param {any} item 
         * @returns 
         */
        filterSex(value, item) {
            return item.sex == value;
        },
        /**
         * 过滤状态
         * 
         * @param {any} value 
         * @param {any} item 
         * @returns 
         */
        filterStatus(value, item) {
            return item.status == value;
        },
        /**
         * 格式化试卷状态
         * 
         * @param {any} item 
         * @returns 
         */
        formatterType(item) {
            for (let i in eType) {
                if (item.etype == eType[i].value) {
                    return eType[i].label;
                }
            }
        },
        /**
         * 过滤试卷状态
         * 
         * @param {any} value 
         * @param {any} item 
         * @returns 
         */
        filterType(value, item) {
            return item.etype == value;
        },
        /**
         * 查询问卷
         * 
         */
        onSelectData() {
            this.setSelectOptions();
            this.getSurvey();
        },
        /**
         * 设置查询问卷的条件
         * 
         */
        setSelectOptions() {
            var selectData = this.$store.state.survey.selectData;
            this.dispatch('set_survey_select_options', {
                selectOptions: {
                    name: selectData.name,
                    etype: selectData.etype,
                    address: resources.formatterAddress(selectData.address),
                    age: selectData.age,
                    phone: selectData.phone
                }
            });
            this.dispatch('set_page_index', { newPageIndex: 1 });
        },
        /**
         * 清空查询问卷的条件
         * 
         */
        onCleanSelectData() {
            this.dispatch('clean_survey_select_options');
            this.getSurvey();
        },
        /**
         * 页码改变的事件
         * 
         * @param {any} currentPage 跳转后页码
         */
        handleCurrentChange(currentPage) {
            this.dispatch('set_page_index', { newPageIndex: currentPage }); //todo
            this.getSurvey();
        },


        /**
         * 获取筛选后结果
         * 
         * @returns 
         */
        getSurvey() {
            var that = this;
            var pageIndex = this.$store.state.survey.currentPage;
            var selectOptions = this.$store.state.survey.selectOptions;
            var options = { page: pageIndex };
            if (selectOptions.name) {
                Object.assign(options, { name: selectOptions.name });
            }
            if (selectOptions.age) {
                Object.assign(options, { age: selectOptions.age });
            }
            if (selectOptions.phone) {
                Object.assign(options, { phone: selectOptions.phone });
            }
            if (selectOptions.etype) {
                Object.assign(options, { etype: selectOptions.etype });
            }
            if (selectOptions.address) {
                Object.assign(options, { address: selectOptions.address });
            }
            SurveyApi.getUsers.call(this, options, data => {
                console.log('data',data)
                that.dispatchSetServeysData(data);
            });
        },
        showDetailDialog(userData) {
            // console.log(userData);
            const answerMap = new Map();
            var answers = [];
            userData.answer.forEach(function(answer) {
                if (answerMap.get(answer.qid)) {
                    var oldAnswer = answerMap.get(answer.qid);
                    oldAnswer.value += (' ' + answer.value);
                } else {
                    answerMap.set(answer.qid, answer);
                }
            }, this);
            for (let value of answerMap.values()) {
                answers.push(value);
            }
            var eyes = null;
            if (userData.eyes) {
                try {
                    eyes = JSON.parse(userData.eyes);
                } catch (err) {

                }
            }
            var leftEye = null;
            var rightEye = null;
            if (eyes) {
                eyes.forEach(eye => {
                    if (eye.eyeType == 1) { //左眼
                        leftEye = eye;
                    } else if (eye.eyeType == 2) { //右眼
                        rightEye = eye;
                    }
                })
            }
            // "[{"colorIndex":2,"colorValue":"#00ff00","eyeType":1,"mediaId":"8h4DJX238DKKoxkwjri_JeJaS554pl6fRYUgBrOudaXqLv11c-MOKWY98c1rgzKe"},{"colorIndex":2,"colorValue":"#00ff00","eyeType":2,"mediaId":"6MBcZQG-oES_BY65nvtQrGxkwtMs-3PUUS169i_sK8MT1_W8kCSfiAyAQd-a6BI5"}]"
            var images = null;
            if (userData.images) {
                images = JSON.parse(userData.images);
            }
            this.detailData = { etype: userData.etype, images, answer: answers, leftEye: leftEye, rightEye: rightEye, name: userData.name };
            this.dialogDetailVisible = true;
        },
        /**
         * 设置某页展示的用户的数据
         * 
         * @param {any} data 
         */
        dispatchSetServeysData(data) {
            // console.log(data);
            this.dispatch('set_serveys_data', { data: data });
        },
        formattIndex(colorIndex) {
            var color = {
                0: '正常',
                1: '眼红',
                2: '眼蓝',
                3: '眼绿',

            };
            return color[colorIndex];
        },
        /**
         * 分发事件
         * 
         * @param {any} name 
         * @param {any} parameter 
         */
        dispatch(name, parameter) {
            this.$store.dispatch(name, parameter);
        },
        formatterTime(time) {
            return new Date(time * 1000).toLocaleString();
        }
    },
    created() {
        this.getSurvey();
    }
}